//最上层的功能模块，用以返回相应的网页资源，写入相应的http response当中
#pragma once
#include <iostream>
#include <string>
#include "Protocol.hpp"
#include "Logger.hpp"
class Http
{
    public:
    Http()
    {}
    
    HttpResponse Exec(HttpRequest& request)
    {
        //通过request构建出相应的response

        //设置http版本
        HttpResponse hr;
        hr.SetVersion(request);
        
        //获取请求资源路径，并读取资源
        hr.ReadContent(request);

        //设置状态码和状态码描述
        if(hr.GetPath() == missing_path)
            hr.SetCode(404);
        else
            hr.SetCode(200);
        hr.CodeToDsc();
        
        //设置相应header:Content-Length;Content-Type
        hr.SetKV("Content-Length",std::to_string(hr.GetBodySize()));
        auto pos = hr.GetPath().find('.');
        if(pos == std::string::npos)
        {
            LOG(LogLevel::ERROR) << "find suffix error";
            return HttpResponse();
        }
        hr.SetKV("Content-Type",hr.SuffixToType(hr.GetPath().substr(pos)));
        return hr;        
    }    
    ~Http()
    {}
};